package cn.ilikes.tools.nosql.jpa.unitls.lists;

import java.util.ArrayList;

import cn.ilikes.tools.nosql.jpa.JPASQLPersistenUnitEnv;
import cn.ilikes.tools.nosql.jpa.clients.AbClient;
import cn.ilikes.tools.nosql.jpa.clients.JPASQLClientsException;
import cn.ilikes.tools.nosql.jpa.clients.indexs.ABClientIndex;
import cn.ilikes.tools.nosql.jpa.clients.indexs.IClientIndex;
import cn.ilikes.tools.nosql.jpa.clients.indexs.units.LuceneQueryUtils;
import cn.ilikes.tools.nosql.jpa.clients.indexs.units.LuceneUtils;
import cn.ilikes.tools.nosql.jpa.metadatas.EntityMetaData;
import cn.ilikes.tools.nosql.jpa.metadatas.elements.EntityFields;
import cn.ilikes.tools.nosql.jpa.metadatas.elements.JPAField;
import cn.ilikes.tools.nosql.jpa.metadatas.elements.Relation.ForeignKey;
import cn.ilikes.tools.nosql.jpa.unitls.PropertyAccessorHelper;

@SuppressWarnings("unchecked")
public class JPArraryList extends ArrayList {

	private static final long serialVersionUID = -7257733173761118275L;

	private Object entity;

	private AbClient client;

	private EntityMetaData entityMetaData;

	private Object keyId;

	private EntityFields entityFields;

	private JPASQLPersistenUnitEnv persistenUnitEnv;

	public JPArraryList(AbClient client, EntityFields entityFields, Object entity, JPASQLPersistenUnitEnv persistenUnitEnv) {
		super();
		this.entity = entity;
		this.client = client;
		this.persistenUnitEnv = persistenUnitEnv;
		this.entityFields = entityFields;
		init();
	}

	private void init() {
		this.entityMetaData = persistenUnitEnv.getMetaDataManagerFactory().selectIEntityMetaData(entity.getClass());
		Object keyId = PropertyAccessorHelper.getId(entity, entityMetaData);
		this.setKeyId(keyId);

	}

	private String queryForSize() {
		JPAField jpafield = entityFields.getjPAField();
		ForeignKey foreignKey = jpafield.getRelation().getType();
		if (!foreignKey.equals(ForeignKey.ONE_TO_MANY))
			throw new JPArraryListRunTimeException("this  entityFields " + entityFields.getFieldName() + " not @ one2Many");
		Class<?> clazz = PropertyAccessorHelper.getGenericClass(entityFields.getField());
		EntityMetaData entityMetaData = persistenUnitEnv.getMetaDataManagerFactory().selectIEntityMetaData(clazz);
		EntityFields entityFields_ = entityMetaData.getEntityField(jpafield.getRelation().getMappedBy());
		String entityindexName = entityMetaData.getEntityIndex().getIndexName();
		String columnName = entityFields_.getIndexField().getName();
		String luceneFieldsName = LuceneUtils.createLuceneFieldsNames(entityindexName, columnName);
		String entityClazz = LuceneQueryUtils.toLowerCase(entityMetaData.getEntityClazz().getCanonicalName());
		String queryForSize = "+" + luceneFieldsName + ":" + keyId + " AND +" + IClientIndex.ENTITY_CLASS_FIELD + ":" + entityClazz;
		return LuceneQueryUtils.toLowerCase(queryForSize);

	}

	public Object getKeyId() {
		return keyId;
	}

	public void setKeyId(Object keyId) {
		this.keyId = keyId;
	}

	@Override
	public int size() {
		try {
			ABClientIndex clientIndex = client.getClientIndex();
			String query = queryForSize();
			int xxx = Long.valueOf(clientIndex.searchSize(query)).intValue();
			return xxx;
		} catch (JPASQLClientsException e) {
			throw new JPArraryListRunTimeException(e);
		}
	}

}
